package com.sunda.spmsuser.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.sunda.spmsuser.entity.SpmsRole;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author Wayne
 * @since 2021-01-25
 */
@Mapper
@Component
public interface SpmsRoleMapper extends BaseMapper<SpmsRole> {

    @Select("SELECT r.UUID AS \"uuid\",\n" +
            "       r.ROLE_ID AS \"roleId\",\n" +
            "       r.ROLE_CN_NAME AS \"roleCnName\",\n" +
            "       r.ROLE_EN_NAME AS \"roleEnName\",\n" +
            "       r.ROLE_INFO AS \"roleInfo\"\n" +
            "FROM SPMS_USER u\n" +
            "    LEFT JOIN SPMS_USER_ROLE ur ON u.WORK_CODE = ur.WORK_CODE\n" +
            "    LEFT JOIN SPMS_ROLE r ON r.UUID = ur.ROLE_UUID\n" +
            "WHERE r.ROLE_ID IS NOT NULL AND u.WORK_CODE = #{userId}")
    List<Map<String, Object>> getUserRoleById(@Param("userId") String userId);

    @Select("SELECT SR.UUID AS \"uuid\",\n" +
            "       SR.ROLE_ID AS \"roleId\",\n" +
            "       SR.ROLE_CN_NAME AS \"roleCnName\",\n" +
            "       SR.ROLE_EN_NAME AS \"roleEnName\",\n" +
            "       SR.ROLE_INFO AS \"roleInfo\",\n" +
            "       SR.CREATE_USER AS \"createUser\",\n" +
            "       U.LAST_NAME AS \"createUserName\",\n" +
            "       SR.CREATE_TIME AS \"createTime\",\n" +
            "       SR.UPDATE_USER AS \"updateUser\",\n" +
            "       SU.LAST_NAME AS \"updateUserName\",\n" +
            "       SR.UPDATE_TIME AS \"updateTime\"\n" +
            "FROM SPMS_ROLE SR\n" +
            "    LEFT JOIN SPMS_USER U ON SR.CREATE_USER = U.WORK_CODE\n" +
            "    LEFT JOIN SPMS_USER SU ON SR.UPDATE_USER = SU.WORK_CODE\n" +
            "ORDER BY SR.ROLE_ID ASC")
    List<Map<String, Object>> selectRoleList();
}
